package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.wanmao.test.bean.XSJLBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class XSJLService {
    private DBOpenHelper dBOpenHelper;

    public XSJLService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }
    public void save(XSJLBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("XSJL_TABLE", null, settingValue(bean));
    }
    private ContentValues settingValue(XSJLBean bean) {
        ContentValues values = new ContentValues();
        values.put("OBJ_ID",bean.getOBJ_ID());
        values.put("BDZMC",bean.getBDZMC());
        values.put("XSRYMC",bean.getXSRYMC());
        values.put("XSKSSJ",bean.getXSKSSJ());
        values.put("XSJSSJ",bean.getXSJSSJ());
        values.put("XSLX",bean.getXSLX());
        values.put("XSJG",bean.getXSJG());
        values.put("XSNR",bean.getXSNR());
        values.put("RWID",bean.getRWID());
        return values;
    }


    public List<XSJLBean> findByRWID(String RWID){
        String sql = "select * from XSJL_TABLE where RWID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{RWID});
        return toListBean(cursor);

    }
    public XSJLBean findByOBJID(String OBJID){
        String sql = "select * from XSJL_TABLE where OBJ_ID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{OBJID});
        XSJLBean bean=new XSJLBean();
        while (cursor.moveToNext()){
            bean.setOBJ_ID(cursor.getString(cursor.getColumnIndex("OBJ_ID")));
            bean.setBDZMC(cursor.getString(cursor.getColumnIndex("BDZMC")));
            bean.setXSRYMC(cursor.getString(cursor.getColumnIndex("XSRYMC")));
            bean.setXSKSSJ(cursor.getString(cursor.getColumnIndex("XSKSSJ")));
            bean.setXSJSSJ(cursor.getString(cursor.getColumnIndex("XSJSSJ")));
            bean.setXSLX(cursor.getString(cursor.getColumnIndex("XSLX")));
            bean.setXSJG(cursor.getString(cursor.getColumnIndex("XSJG")));
            bean.setXSNR(cursor.getString(cursor.getColumnIndex("XSNR")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
        }
        return bean;
    }

    private List<XSJLBean> toListBean(Cursor cursor){
        List<XSJLBean> list=new ArrayList<>();
        while (cursor.moveToNext()){
            XSJLBean bean=new XSJLBean();
            bean.setOBJ_ID(cursor.getString(cursor.getColumnIndex("OBJ_ID")));
            bean.setBDZMC(cursor.getString(cursor.getColumnIndex("BDZMC")));
            bean.setXSRYMC(cursor.getString(cursor.getColumnIndex("XSRYMC")));
            bean.setXSKSSJ(cursor.getString(cursor.getColumnIndex("XSKSSJ")));
            bean.setXSJSSJ(cursor.getString(cursor.getColumnIndex("XSJSSJ")));
            bean.setXSLX(cursor.getString(cursor.getColumnIndex("XSLX")));
            bean.setXSJG(cursor.getString(cursor.getColumnIndex("XSJG")));
            bean.setXSNR(cursor.getString(cursor.getColumnIndex("XSNR")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
            list.add(bean);
        }
        return list;
    }
}
